﻿using RAP.Framework.Libary.Utils;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace RAP.Framework.Libary.SqlTaskParser.Parsers
{
  /// <summary>
  /// 表达式语法解析基类
  /// </summary>
  public abstract class ExpressionTokenParser : TokenParserBase
  {
    protected static Regex _expTestRegx = new Regex(" and | or |=|<>");

    /// <summary>
    /// 将当前指令处理为@await模式，即等待前面的任务完成之后再执行该指令
    /// </summary>
    /// <param name="context"></param>
    protected void DoAwait(SqlParseContext context)
    {
      //将当前并行任务集合添加到所有任务集合队列中
      context.ParseResult.SqlTasks.Add(context.CurrentParallelTasks);

      //创建一个新的并行任务集合
      context.CurrentParallelTasks = new SqlTaskCollection { Await = true };
    }

    /// <summary>
    /// 处理C#表达式
    /// </summary>
    /// <param name="expression"></param>
    /// <returns></returns>
    protected virtual string ParseCSharpExpression(string expression)
    {
      if (expression.IsNotNullOrEmpty())
      {
        if (_expTestRegx.IsMatch(expression))
        {
          expression = expression.Replace(" and ", " && ").Replace(" or ", " || ").Replace("=", " == ").Replace("<>", "!=");
        }
      }
      return expression;
    }
  }
}
